package com.lovo.csc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.commom.util.ConstantUtil;
import com.lovo.csc.service.ISchemeService;
import com.lovo.csc.bean.SchemeBean;
import com.lovo.csc.mapper.ISchemeMapper;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;


@Service
@Transactional
public class SchemeServiceImpl implements ISchemeService {
    @Autowired
    private ISchemeMapper schemeMapper;

    /**
     * 根据审核状态分页查询审核信息
     * @param pageNO 当前页数
     * @param status 审核状态
     * @return
     */
    @Override
    public IPage<SchemeBean> shenhecaigouxinxi(Integer pageNO,int status) {
        QueryWrapper<SchemeBean> qw=new QueryWrapper<>();
        qw.eq("s_status",status);
        return schemeMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    /**
     * 根据方案编号修改状态
     * @param idNum 申请方案编号
     * @param status 审核状态
     */
    @Override
    public void updateById(Long idNum,int status) {
        QueryWrapper<SchemeBean> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("s_idNum", idNum);
        SchemeBean scheme=schemeMapper.selectOne(queryWrapper);
        scheme.setStatus(status);
        scheme.setDealDate(LocalDate.now());
        schemeMapper.updateById(scheme);
    }

    /**
     * 根据方案编号查询详细信息
     * @param idNum
     * @return
     */
    @Override
    public SchemeBean findById(Long idNum) {
        QueryWrapper<SchemeBean> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("s_idNum", idNum);
       return schemeMapper.selectOne(queryWrapper);
    }

    /**
     * 根据编号加入审核不通过理由
     * @param idNum
     * @param text
     */
    @Override
    public void updatetext(Long idNum, String text) {
        QueryWrapper<SchemeBean> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("s_idNum", idNum);
        SchemeBean scheme=schemeMapper.selectOne(queryWrapper);
        scheme.setRefuse(text);
        scheme.setDealDate(LocalDate.now());
        schemeMapper.updateById(scheme);
    }
    @RabbitListener(queues = "queueSheme")
    public void getScheme(String json) {
        System.err.println(json);
        SchemeBean schemeBean= JSONObject.parseObject(json, SchemeBean.class);
        schemeMapper.insert(schemeBean);
        ConstantUtil.WS_MSG.add("采购审核");
    }
}
